<?php
header("Content-Type: text/html; charset=iso-8859-1");

class table_field{
	
	function table_field($name,$tablename = ""){
		$this->tablename = $tablename;
		$this->print = $name;
		$this->name = $name;
		$this->hidden =  0;
		$this->onclick = "onclick = \"sort_table('$this->name','$tablename')\" style = 'cursor: pointer'";		
		$this->styleclass = "";
	}
	
}

class table_row{
	function table_row(){
		$this->hidden = 0;
		$this->onclick = "";
		$this->cols = array();
		$this->styleclass = "";
	}
	
	function hide(){
		$this->hidden = 1;
	}
	
	function show(){
		$this->hidden = 0;
	}
}

class table{

	function table($tablename = ""){
		if(isset($_SESSION['table'.$tablename])) unset($_SESSION['table'.$tablename]);	
		$this->tablename = $tablename;		
		$this->fields = array();
		$this->order = "";
		$this->sense = "ASC";
		$this->nb_rows = 30;
		$this->rows = array();
		$this->start = 1;
		$this->styleclass = "";
		$this->filter = 0;
		$this->filter_size = 20;
		$this->filter_value = "";
		$this->id_prefix = "";
		$this->td_ids = array();
		$this->sortable = 0;
		$this->tbody_id = "table_content_$tablename"; #never change unless used for "sortable".
	}
	
	function sort(){
		if($this->order && count($this->rows)){
			foreach($this->rows as $key =>  $row){
				foreach($this->fields as $field){
					if(isset($row->cols))${$field->name}[$key] = $row->cols[$field->name];
				}
			}
			if($this->sense == 'ASC'){
				array_multisort(${$this->order}, SORT_ASC, $this->rows);				
			}
			else{
				array_multisort(${$this->order}, SORT_DESC, $this->rows);				
			}
		}
	}
	
	function filter(){
		$col_nb = 0;
		foreach($this->fields as $field){
			if(!$field->hidden){
				$col_nb++;
			} 
		}
		return "<tr><td class = 'center' colspan = '$col_nb'><span class = 'bold'>Filtrer</span> : <input type = 'text' size = '20' name = 'filter' value =  '$this->filter_value' onkeyup = \"table_filter(this.value,'$this->tablename')\"  id = 'table_filter_$this->tablename' /><img src = 'images/icon_close.png' alt = 'x' width = '12' height = '12' onclick = \"table_filter('','$this->tablename');document.getElementById('table_filter_$this->tablename').value = '';\"  class = 'pointer'/></td></tr>";
	}

	function print_up(){
		$_SESSION['table'.$this->tablename] = serialize($this);
		$tds = "";
		for($i = 0; $i < count($this->fields)-1; $i++){
			if($this->fields[$i]->hidden == 0) $tds .= "<td></td>\n";
		} 
		return "<tr>$tds<td class = 'right' nowrap><span class = 'suivant pointer' onclick = \"update_table('".($this->start - $this->nb_rows)."','$this->tablename')\">pr&eacute;c&eacuted. &uarr;</span></td></tr>\n\n";		
	}
	function print_down(){
		$_SESSION['table'.$this->tablename] = serialize($this);
		$tds = "";
		for($i = 0; $i < count($this->fields)-1; $i++){
			if($this->fields[$i]->hidden == 0) $tds .= "<td></td>\n";	
		} 
		return "<tr>$tds<td class = 'right' nowrap><span class = 'suivant pointer' onclick = \"update_table('".($this->start + $this->nb_rows)."','$this->tablename')\">suivant &darr;</span></td></tr>";	
return "";
	}
	
	function print_header(){
		$print =  "<table class = '$this->styleclass'><thead><tr>\n";
		$nb_fields = 0;
		foreach($this->fields as $field){
			// if(!$field->hidden) $print .=  "<th $field->onclick >".(str_replace('_',' ',$field->print))."</th>\n";	
			if(!$field->hidden){ 
				$nb_fields++;
				$print .=  "<th $field->onclick >".($field->print)."</th>\n";				
			}
		} 
		$print .=  "</tr>\n";
		if($this->filter) $print .= $this->filter();
		$print .=  "</thead>\n";
		$print .=  "<tfoot><tr><td colspan = '$nb_fields'></td></tr></tfoot>\n";
		$print .=  "<tbody id = '$this->tbody_id'>\n";		
		return $print;
	}

	function print_body($two_color=0){
		$_SESSION['table'.$this->tablename] = serialize($this);		
#		$max = count($this->rows);
		$pos = 0;
		$print = "";
		$this->sort();
		$classes = array();
		$hiddens = array();
		foreach($this->fields as $field){
			$classes[$field->name] = $field->styleclass;
			$hiddens[$field->name] = $field->hidden;			
		}
		for($i = 0; $i < count($this->rows); $i++){
			if($this->rows[$i]->hidden == 0 && count($this->rows[$i]->cols)){
				$pos++;
				if($pos >= $this->start && $pos < $this->start + $this->nb_rows && count($this->rows[$i]->cols)){
					$sortable = ($this->sortable) ? "id = 'sort".$this->sortable."_".$this->rows[$i]->cols[$this->sortable]."' " : "";
					$print .= "<tr class = '".$this->rows[$i]->styleclass."' $sortable  ".$this->rows[$i]->onclick.">";
					foreach($this->rows[$i]->cols as $field => $value){
						$td_id = (!empty($this->td_ids[$field])) ? "id = '".$this->tablename."_".$field."_".$this->rows[$i]->cols[$this->td_ids[$field]]."'" : "";
						$class = $classes[$field];
						$value = (ereg("[0-9]{4}-[0-9]{2}-[0-9]{2}",$value)) ? format_date($value) : $value;
						$value = (ereg("[0-9]{2}:[0-9]{2}:[0-9]{2}",$value)) ? format_heure($value) : $value;					
						if(!$hiddens[$field]) $print .= "<td class = '$class' $td_id>".$value."</td>\n";	
					} 
					$print = substr($print,0,-1);
					$print .= "</tr>\n\n";			
				}
			}
			// else error_log($i." hidden:".$this->rows[$i]->hidden." :: nb_cols=".count($this->rows[$i]->cols));
		}
		if(!$print) $print = "<tr><td colspan = '".(count($this->fields))."' class = 'fade center'>aucune donn&eacute;e</td></tr>\n\n";
		if($this->start >= $this->nb_rows) $print = $this->print_up().$print;		
		if($this->start + $this->nb_rows < $pos) $print .= $this->print_down();
		else $print = substr($print,0,-2);
		return $print;
	}
	
	function print_footer($row = ""){
		return "$row</tbody></table>\n";
	}
}
?>